package com.ruoyi.framework.mqtt.handler;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.framework.mqtt.gateway.IMqttSender;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.instrument.domain.InsCalibrationData;
import com.ruoyi.project.instrument.domain.InsCalibrationDataList;
import com.ruoyi.project.instrument.service.IInsCalibrationDataListService;
import com.ruoyi.project.instrument.service.IInsCalibrationDataService;
import com.ruoyi.project.system.domain.SysDictData;
import com.ruoyi.project.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Taney
 */
@Component
public class QueryCalibrationDataHandler extends AbstractHandler {

    public static final String TOPIC = "/calibrationData/query";

    @Autowired
    IInsCalibrationDataService iInsCalibrationDataService;
    @Autowired
    IInsCalibrationDataListService iInsCalibrationDataListService;
    @Autowired
    ISysDictDataService iSysDictDataService;
    /**
     * 注入发送MQTT的Bean
     */
    @Resource
    IMqttSender iMqttSender;

    @Override
    public void handleMessage(Message message, String topic, String msg) throws MessagingException {
        if (matches(TOPIC, topic)) {
            ClientInfo clientInfo = getClientInfo(topic);
            String product = clientInfo.getProduct();
            String deviceId = clientInfo.getDeviceId();
            logger.debug("设备[{}-{}]查询定标曲线【{}】", product, deviceId, msg);
            /*
            {
               "codeNo":试剂项目ID,
                "lotNo":批号
            }
             */
            if (isJson(msg)) {
                String machineType = "";
                SysDictData sysDictData = new SysDictData();
                sysDictData.setDictType("machine_type");
                List<SysDictData> machineTypeList = iSysDictDataService.selectDictDataList(sysDictData);
                for (SysDictData item : machineTypeList) {
                    if (item.getDictLabel().equals(product)) {
                        machineType = item.getDictValue();
                        break;
                    }
                }
                InsCalibrationData insCalibrationData = JSONUtil.toBean(msg, InsCalibrationData.class);

                LambdaQueryWrapper<InsCalibrationData> insCalibrationDataqueryWrapper = new LambdaQueryWrapper<>();
                insCalibrationDataqueryWrapper.eq(InsCalibrationData::getMachineType, machineType);
                insCalibrationDataqueryWrapper.eq(InsCalibrationData::getCodeNo, insCalibrationData.getCodeNo());
                insCalibrationDataqueryWrapper.eq(InsCalibrationData::getLotNo, insCalibrationData.getLotNo());
                InsCalibrationData insCalibrationDataFind = iInsCalibrationDataService.getOne(insCalibrationDataqueryWrapper);
                List<InsCalibrationDataList> list = new ArrayList<>();
                JSONObject jsonObject = JSONUtil.createObj();
                if (insCalibrationDataFind != null) {
                    LambdaQueryWrapper<InsCalibrationDataList> insCalibrationDataListLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    insCalibrationDataListLambdaQueryWrapper.eq(InsCalibrationDataList::getCalibrationDataId, insCalibrationDataFind.getId());
                    list = iInsCalibrationDataListService.list(insCalibrationDataListLambdaQueryWrapper);
                    jsonObject.put("codeNo", insCalibrationDataFind.getCodeNo());
                    jsonObject.put("lotNo", insCalibrationDataFind.getLotNo());
                } else {
                    logger.debug("没有查到{}-{}-{},的定标记录", product, insCalibrationData.getCodeNo(), insCalibrationData.getLotNo());
                }
                jsonObject.put("calibrationData", list);
                iMqttSender.sendToMqtt("/device/" + product + "/" + deviceId + "/calibrationData/replay", JSONUtil.toJsonStr(AjaxResult.success(jsonObject)));
            }
        }
    }


}
